<template>
  <div class='update-birthday'>
    <van-datetime-picker v-model="currentDate" type="date" title="选择年月日" :min-date="minDate"
      :max-date="maxDate" @confirm="onConfirm" @cancel="$emit('close')" />
  </div>
</template>
<script>
import { editUserProfile } from '@/api/user';
import dayjs from 'dayjs'
export default {
  name: 'UpdateBirthday',
  components: {},
  props: {
    value: {
      type: String,
      required: true,
    },
  },
  data() {
    return {
      minDate: new Date(1990, 0, 1),
      maxDate: new Date(2025, 10, 1),
      currentDate: new Date(this.value),
    };
  },
  created() {},
  computed: {},
  methods: {
    onConfirm() {
      // toast提示
      this.$toast.loading({
        message: '更新中...',
        forbidClick: true, // 禁用背景点击
        duration: 0, // 展示时长(ms)，值为 0 时，toast 不会消失
      });
      const currentDate = dayjs(this.currentDate).format('YYYY-MM-DD');
      console.log(currentDate);
      editUserProfile({
        birthday: currentDate,
      })
        .then((res) => {
          console.log(res);
          // 成功提示
          this.$toast('更新成功');
          // 关闭弹出层
          this.$emit('close');
          // 更新视图
          this.$emit('input', currentDate);
        })
        .catch((err) => {
          this.$toast.fail(err || '更新失败');
          console.log(err);
        });
    },
  },
};
</script>
<style lang='less' scoped>
</style>
